<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.pa_id ? '编辑' : '添加'" v-model="visible" align-center :z-index="2000"
    width="800px" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :rules="rules" :validate-on-rule-change="false"
      :label-width="140">
      <div class="grid grid-cols-9 gap-x-3">
        <el-form-item label="平台类型" prop="platform_type" class="col-span-4">
          <VSelect v-model="form.platform_type" placeholder="请选择" :options="options.platform_type" filterable clearable
            class="w-full" />
        </el-form-item>
        <el-form-item label="账号名称" prop="account_name" class="col-span-4">
          <el-input v-model="form.account_name" placeholder="账号名称" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2" active-text="启用" inactive-text="禁用"
            inline-prompt />
        </el-form-item>
      </div>
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="采购人员" prop="purchase_account_id">
          <VCascader v-model="form.purchase_account_id" placeholder="采购人员" multiple collapse-tags filterable clearable
            class="w-full" @change="({ labels }) => (form.purchase_account = labels)" />
        </el-form-item>
        <el-form-item label="下单支付宝账号" prop="alipay_account_id">
          <VSelect v-model="form.alipay_account_id" placeholder="请选择" :options="alipayOptions" filterable clearable
            class="w-full" />
        </el-form-item>
        <el-form-item label="账号类型" prop="account_type">
          <VSelect v-model="form.account_type" placeholder="请选择" :options="options.account_type" filterable clearable
            class="w-full" />
        </el-form-item>
        <el-form-item label="公司主体" prop="company_id">
          <VSelect v-model="form.company_id" placeholder="请选择" :options="options.company_id" filterable clearable
            class="w-full" />
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive } from 'vue';
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
const alipayOptions = ref([])
const purchaseOptions = ref([{ label: '人员1', value: '1' }])
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})
const rules = reactive({
  platform_type: [{ required: true, message: '不能为空', trigger: 'change' }],
  account_name: [{ required: true, message: '不能为空' }],
  purchase_account_id: [{ required: true, message: '不能为空', trigger: 'change' }],
  company_id: [{ required: true, message: '不能为空', trigger: 'change' }],
  status: [{ required: true, message: '不能为空', trigger: 'change' }],
})


// 打开弹窗
const open = async (id) => {
  if (!load.value) {
    load.value = true
    api.getAlipay().then(res => {
      alipayOptions.value = res.data.map(d => ({ label: d.alipay_account, value: d.aa_id }))
    })
    await 1
  }
  form.value = {
    pa_id: '',
    account_name: '',
    platform_type: '',
    purchase_account_id: '',
    purchase_account: '',
    account_type: '',
    alipay_account_id: '',
    company_id: '',
    status: 1
  }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
  // 编辑
  if (id) {
    const res = await api.detail({ pa_id: id })
    const { pa_id, account_name, platform_type, purchase_account_id, purchase_account, account_type, alipay_account_id, company_id, status } = res.data
    form.value = { pa_id, account_name, platform_type, purchase_account_id, purchase_account, account_type, alipay_account_id, company_id, status }
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  const validator = await formRef.validate()
  if (!validator) return
  loadings.create = true
  api[form.value.pa_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>